<?php
/**
 * @title 97. 交错字符串
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $s1
     * @param String $s2
     * @param String $s3
     * @return Boolean
     */
    function isInterleave($s1, $s2, $s3) {
        /**
         * @since 2024-01-02 字符串长度
         */
        $this->len1 = strlen($s1);
        $this->len2 = strlen($s2);
        $this->len3 = strlen($s3);
        $this->s1 = $s1;
        $this->s2 = $s2;
        $this->s3 = $s3;

        /**
         * @since 2024-01-02 长度不能匹配
         */
        if($this->len1 + $this->len2 != $this->len3){
            return false;
        } else {}

        /**
         * @since 2024-01-02 避免重复遍历
         */
        $this->dp = [];

        /**
         * @since 2024-01-02 2个指针
         * @return
         */
        return $this->IsConnect(0, 0);
    }

    /**
     * @title 2个指针移动判断，s3是否由s1+s2组合而成
     * @author start2004
     * @since 2024-01-02 Created
     *
     * @param Integer $p1
     * @param Integer $p2
     * @return boolean
     */
    function IsConnect($p1, $p2){
        /**
         * @since 2024-01-02 是否命中缓存
         */
        $key = "{$p1}-{$p2}";
        if(isset($this->dp[$key])){
            return $this->dp[$key];
        } else {}

        /**
         * @since 2024-01-02 p3指针
         */
        $p3 = $p1+$p2;
        if($p3 == $this->len3){
            return true;
        } else {}

        /**
         * @since 2024-01-02 p1和p3指针元素相同
         */
        if($p1<$this->len1 AND $this->s1[$p1] == $this->s3[$p3]){
            /**
             * @since 2024-01-02 移动指针，继续查找
             */
            $connectResult = $this->IsConnect($p1+1, $p2);
            if($connectResult === true){
                /**
                 * @return Boolean
                 */
                return $this->dp[$key] = true;
            } else {}
        }

        /**
         * @since 2024-01-02 p2和p3指针元素相同
         */
        if($p2<$this->len2 AND $this->s2[$p2] == $this->s3[$p3]){
            /**
             * @since 2024-01-02 移动指针，继续查找
             */
            $connectResult = $this->IsConnect($p1, $p2+1);
            if($connectResult === true){
                /**
                 * @return Boolean
                 */
                return $this->dp[$key] = true;
            } else {}
        }

        /**
         * @since 2024-01-02 匹配失败
         * @return Boolean
         */
        return $this->dp[$key] = false;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.02_8.php
 */
$datas = [
    ["aabcc", "dbbca", "aadbbcbcac"], // true
    ["aabcc", "dbbca", "aadbbbaccc"], // false
    ["", "", ""], // true
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->isInterleave(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
